// pages/doctor-goods-add-3/doctor-goods-add-3.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    productDesc: '', // 产品描述
    productPic: '', // 医美产品图片
    images: [], // wx路径
    imagesurl: [], // 上传成功的图片地址
    options: '',
    showimages: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var curr = getCurrentPages();
   
    this.setData({
      'options': options,
      'showimages': JSON.parse(options.goodsImages),
      'imagesurl': JSON.parse(options.goodsImages),
      'productDesc': options.productDesc,
      'cityType': options.cityType,
      'deviceId': options.deviceId,
      'narcosis': options.narcosis,
    });
  },

  // 添加补充信息
  addProductDesc: function(e) {
    var productDesc = e.detail.value;
    this.setData({
      'productDesc': productDesc,
    })
  },

  // 上传图片
  chooseImg: function(e) {
    var that = this;
    var imgLength = this.data.showimages.length;
   
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      count: 5 - imgLength,
      success: function(res) {
        // 将现有和新上传的合并
        // var images = that.data.images.concat(res.tempFilePaths); // 合并原有数组和现传照片
        var oldcount = that.data.showimages.length;
        var newcount = res.tempFilePaths.length + oldcount;
    
        var images = newcount <= 5 ? res.tempFilePaths : res.tempFilePaths.slice(0, (5 - oldcount)); // 取前5张
       
        that.setData({
          'images': images,
        })
       
        that.uploadimages(); // 选择图片后 上传到服务器上
      },
    });
  },

  // 上传图片
  uploadimages: function(e) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
    var images = this.data.images;
    var imagesArr = [];
    for (var i = 0; i < images.length; i++) {
      app.uploadFile({
        // url:'doctor/product/uploadFile',
        url: 'doctor/docExample/uploadFile',
        filePath: images[i],
        success: function(res) {
          var path = JSON.parse(res.data).data[0].filePath;
         
          var imagesurl = that.data.imagesurl;
          var showimages = that.data.showimages
          imagesurl.push(path);
          showimages.push(path);
          that.setData({
            'imagesurl': imagesurl,
            'showimages': showimages,
          })
        }
      })
    };
  },

  //  预览图片
  handleImagePreview: function(e) {
    var index = e.target.dataset.index;
    var showimages = this.data.showimages;
    wx.previewImage({
      current: showimages[index],
      urls: showimages,
    })
  },

  // 删除图片
  handleDeleteImage: function(e) {
    var index = e.target.dataset.index;
    var imagesurl = this.data.imagesurl;
    var showimages = this.data.showimages;
    imagesurl.splice(index, 1);
    showimages.splice(index, 1);
    this.setData({
      'imagesurl': imagesurl,
      'showimages': showimages,
    });
   
  },

  // 完成商品添加
  completeAddGoods: function(e) {
    this.handleSubmit();
  },

  handleSubmit: function(e) {

    var that = this;
    // var platformservice = this.data.options.platformservice.split(",");
    // console.log('选定服务项:', platformservice);

    var productId = this.data.options.productId;
    var productpic = this.data.options.productPic;
    var productname = this.data.options.productName;
    var serviceprice = this.data.options.servicePrice;
    var totalprice = this.data.options.totalPrice;
    var makeprice = this.data.options.makePrice;
    var citytype = this.data.options.cityType;
    var deviceId = this.data.options.deviceId;
    var narcosis = this.data.options.narcosis;
   
    app.request({
      url: 'doctor/product/edit',
      data: {
        'productId': productId,
        'productPic': productpic,
        'productName': productname,
        'cityType': citytype,
        'deviceId': deviceId,
        'narcosis': narcosis,
        'servicePrice': serviceprice,
        'totalPrice': totalprice,
        'makePrice': makeprice,
        'contentPic': that.data.imagesurl,
        'productDesc': that.data.productDesc,
      },
      success(res) {
       
        setTimeout(function () {
          wx.redirectTo({
            url: '../doctor-case-edit-5/doctor-case-edit-5?type=2',
          })
        }, 1000)
      }
    })
  },
  onShow:function(){
    console.log(this.data.showimages.length);
  }
})